package com.huli.controller;

import com.huli.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/hz/login")
public class LoginController {
    // 注入SysUserService
    @Autowired
    private SysUserService sysUserService;

    // 手机号登录
    @PostMapping("/loginPhone")
    public String login(@RequestParam("phone") String phone, @RequestParam("captcha") int captcha) throws Exception {
            // 调用sysUserService的login方法进行登录
            boolean login = sysUserService.login(phone, captcha);
        if(!login){
            // 如果登录失败，返回手机号或验证码错误
            return "手机号或验证码错误";
        }else {
            // 如果登录成功，返回登录成功
            return "登录成功";
        }
    }
    // 发送验证码
    @RequestMapping("/crestecaptcha")
    public Map<String, Object> creteCaptcha(@RequestParam String phone) {
        // 创建一个HashMap用于存储返回结果
        HashMap<String, Object> response = new HashMap<>();
        try {
            // 调用sysUserService的creteCaptcha方法发送验证码
            sysUserService.creteCaptcha(phone);
            // 如果发送成功，将success设为true，message设为验证码发送成功
            response.put("success",true);
            response.put("message","验证码发送成功");
        }catch (Exception e){
            // 如果发送失败，将success设为false，message设为异常信息
            response.put("success",false);
            response.put("message",e.getMessage());
        }
        // 返回结果
        return response;
    }
}
